<template>
  <view class="address-box">
    <view class="content">
      <button type="primary" size="mini" class="choose-address" v-if="JSON.stringify(address) === '{}'" @click="chooseAddress">请选择收货地址+</button>
      <view class="choose-address-info" v-else @click="chooseAddress">
        <view class="row1">
          <view class="name-left">
            收货人: {{ address.userName }}
          </view>
          <view class="phone-right">
            电话: {{ address.telNumber }}
            <uni-icons type="arrowright"></uni-icons>
          </view>
        </view>
        <view class="row2">
          <view class="address-title">
            收货地址:
          </view>
          <view class="address-content">
            {{ detailsAddress }}
          </view>
        </view>
      </view>
    </view>
    <view class="border-bottom">
      <view class="border-item" v-for="(item, i) in itemCount" :key="i">
        
      </view>
    </view>
  </view>
</template>

<script>
  import { mapState, mapMutations, mapGetters } from 'vuex'
  export default {
    name:"my-address",
    computed: {
      ...mapState('m_user', ['address']),
      ...mapGetters('m_user', ['detailsAddress'])
    },
    data() {
      return {
        itemCount: [],
      };
    },
    created() {
      // 初始化下面边框的 项数 
      this.initBorderItem()
    },
    methods: {
      ...mapMutations('m_user', ['updateAddress']),
      initBorderItem() {
        for(var i = 0; i < 10; i++) {
          this.itemCount.push(i)
        }
      },
      async chooseAddress() {
        // 使用数组解构
        const [err, addressInfo] = await uni.chooseAddress().catch(err => err)
        if(addressInfo && addressInfo.errMsg == 'chooseAddress:ok') {
          // console.log(addressInfo)
          // 将拿到的address赋值给vuex中的user store 中的 address 属性
          this.updateAddress(addressInfo)
        }
        
        // 如果用户未授权
        if(err && err.errMsg === 'chooseAddress:fail auth deny') {
          // 向用户重新发起授权
          this.reAuth();
        }
      },
      /**
       * 发起授权设置
       */
      async reAuth() {
        const [err, clickResult] = await uni.showModal({
          content: '检测到您没打开地址权限，是否去设置打开？',
          confirmText: '确认',
          cancelText: '取消'
        })
        // 如果弹框异常，直接退出
        if(err) return
        
        if(clickResult.cancel) return uni.$msgShow('您取消了地址授权！')
        // 打开授权设置
        if(clickResult.confirm) return uni.openSetting({
          success: (settingResult) => {
            // 如果授权成功，提示授权成功
            if (settingResult.authSetting['scope.address']) return uni.$showMsg('授权成功！请选择地址')
            // 如果取消授权，提示取消授权
            if (!settingResult.authSetting['scope.address']) return uni.$showMsg('您取消了地址授权！')
          }
        })
      }
    }
  }
</script>

<style lang="scss">
  .content {
    display: flex;
    justify-content: center;
    align-items: center;
    height: 90px;
    .choose-address {
      box-shadow: 0 0 3px #000000;
      padding: 3px 15px;
    }
    
    .choose-address-info {
      display: flex;
      flex-direction: column;
      justify-content: center;
      height: 90px;
      width: 100%;
      padding: 0 5px;
      font-size: 14px;
      
      .row1 {
        display: flex;
        justify-content: space-between;
        margin-bottom: 10px;
      }
      .row2 {
        display: flex;
        .address-title {
          white-space: nowrap;
          margin-right: 5px;
        }
        .address-content {
          font-size: 15px;
        }
      }
    }
  }
  .border-bottom {
    display: flex;
    .border-item {
      position: relative;
      flex: 2;
      height: 5px;
      margin: 0 5px;
      &:nth-of-type(even) {
        background-color: #f63b62;
        &::before {
          content: "";
          position: absolute;
          top: -1px;
          left: -4px;
          width: 0;
          height: 0;
          border: 3px solid transparent;
          border-right-color: #f63b62;
          border-bottom-color: #f63b62;
        }
        &::after {
          content: "";
          position: absolute;
          top: 0;
          right: -4px;
          width: 0;
          height: 0;
          border: 3px solid transparent;
          border-left-color: #f63b62;
          border-top-color: #f63b62;
        }
      }
      &:nth-of-type(odd) {
        background-color: #0685f7;
        &::before {
          content: "";
          position: absolute;
          top: -1px;
          left: -4px;
          width: 0;
          height: 0;
          border: 3px solid transparent;
          border-right-color: #0685f7;
          border-bottom-color: #0685f7;
        }
        &::after {
          content: "";
          position: absolute;
          top: 0;
          right: -4px;
          width: 0;
          height: 0;
          border: 3px solid transparent;
          border-left-color: #0685f7;
          border-top-color: #0685f7;
        }
      }
      &:first-of-type {
        flex: 1;
        &::before {
          border-color: transparent;
        }
      }
      &:last-of-type {
        flex: 1;
        &::after {
          border-color: transparent;
        }
      }
      
    }
  }
</style>
